package edu.gtcfla.km.appmodules.web;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.vsg.biz.BusinessOperationException;
import org.vsg.common.i18n.I18nMessageService;
import org.vsg.common.web.json.SubmitResult;
import org.vsg.security.annotation.RegisterRequestPermissionStrategy;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.istack.internal.logging.Logger;

import edu.gtcfla.km.appmodules.service.UploadFileManager;
import edu.gtcfla.km.appmodules.service.UploadFileService;
import edu.gtcfla.km.appmodules.vo.UploadFileFormVO;

@Controller
@Lazy
public class UploadFileController {
	
	static private Logger logger = Logger.getLogger( UploadFileController.class );

	@Autowired(required = false)
	private UploadFileService uploadFileService;

	private final String modulePath = "/knowledge";

	@Autowired(required = false)
	private UploadFileManager uploadFileManager;

	@Autowired(required = false)
	private I18nMessageService i18nMessageService;

	private ObjectMapper mapper = new ObjectMapper();
	
	@RequiresUser
	@RegisterRequestPermissionStrategy(value =edu.gtcfla.km.core.ApplicationConstant.DATA_UPLOAD + "/colligationAttachment.act", skipCheck = true)
	@RequestMapping(value =  edu.gtcfla.km.core.ApplicationConstant.DATA_UPLOAD + "/colligationAttachment.act")
	public ModelAndView uploadMultiFileForAttachment(HttpServletResponse res,
			@RequestParam("Filedata") MultipartFile file , String docId) throws Exception {

		// --- map to json object data ---
		SubmitResult result = new SubmitResult();
		
		try {

			// --- handle file body --
			UploadFileFormVO uffv = uploadFileManager.saveOne(file);
			if (uffv != null) {
				// --- insert file ---
				uploadFileService.addUploadfileToDocId(uffv , docId);

			}

			result.setSuccess(true);
			String msg = i18nMessageService.getMessage(
					"module-km.uploadfile.success", null);
			result.setMsg(msg);

		} catch (BusinessOperationException e) {

			e.printStackTrace();
			result.setSuccess(false);
			String msg = i18nMessageService.getMessage(
					"module-client.company.add.error", null);
			result.setMsg(msg);

		}

		byte[] content = mapper.writeValueAsBytes(result);

		String contentOutput = new String(content);

		ModelAndView mav = new ModelAndView();
		mav.addObject("json", contentOutput);
		mav.setViewName("result"); // view name
		return mav;

	}

	@RequiresUser
	@RegisterRequestPermissionStrategy(value =edu.gtcfla.km.core.ApplicationConstant.DATA_UPLOAD + "/attachment.act", skipCheck = true)
	@RequestMapping(value =  edu.gtcfla.km.core.ApplicationConstant.DATA_UPLOAD + "/attachment.act")
	public ModelAndView uploadMultiFiles(HttpServletResponse res, @RequestParam("Filedata") MultipartFile file , String categoryId) throws Exception {

		// --- map to json object data ---
		SubmitResult result = new SubmitResult();
		


		try {
			// --- handle file body --
			UploadFileFormVO uffv = uploadFileManager.saveOne(file);
			if (uffv != null) {
				// --- set vo type ---

				uffv.setCategoryId(categoryId);

				// --- insert file ---

				uploadFileService.addUploadFile(uffv);

			}

			result.setSuccess(true);
			String msg = i18nMessageService.getMessage(
					"module-km.uploadfile.success", null);
			result.setMsg(msg);

		} catch (BusinessOperationException e) {

			e.printStackTrace();
			result.setSuccess(false);
			String msg = i18nMessageService.getMessage(
					"module-client.company.add.error", null);
			result.setMsg(msg);

		}

		byte[] content = mapper.writeValueAsBytes(result);

		String contentOutput = new String(content);

		ModelAndView mav = new ModelAndView();
		mav.addObject("json", contentOutput);
		mav.setViewName("result"); // view name
		return mav;

	}

}
